<template>
  <div>
    <!-- 整车物料价格查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="zjmaterialPrice"
        :isSelectColumn="false"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle(4)">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="visible" :title="'基本信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ChildrenModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isSearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownerclose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getDealerUser, excelList1, vehicleConfiguration, exporturl } from '@/api/omSale/vehicleParameters/materialPrice/index.js';
import { dictionary } from '@/utils/dictMixin';
import ChildrenModel from './childrenModel.vue';
import ownerSeacher from './ownerSeacher';
import { exportExcelFile } from '@/utils';
export default {
  name: 'zjvehicleMaterials',
  components: {
    ChildrenModel,
    ownerSeacher
  },
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        xhrAbort: true
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      visible: false,
      addvisible: false,
      IssueVisible: false,
      orderVisible: false,
      isOpen: true,
      isSearch: false,
      selectList: []
    };
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getUser();
    this.getcarList();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '公告号',
          fieldName: 'noticeNo',
          placeholder: '请输入'
        },
        // {
        //   type: 'SELECT',
        //   label: '启用标记',
        //   fieldName: 'isValid',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004')
        // },
        // {
        //   type: 'SELECT',
        //   label: '是否可订购',
        //   fieldName: 'orderStatus',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004')
        // },
        // {
        //   type: 'SELECT',
        //   label: '是否下发',
        //   fieldName: 'isIssue',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004')
        // },
        // {
        //   type: 'INPUT',
        //   label: '经销商代码',
        //   fieldName: 'dealerCode',
        //   placeholder: '请选择',
        //   unitRender: () => {
        //     return (
        //       <div>
        //         <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
        //       </div>
        //     );
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '请输入'
        // }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'column-action',
        //   width: 50,
        //   fixed: 'left',
        //   render: (props, h) => {
        //     return (
        //       <div>
        //         <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
        //           详情
        //         </multiuse-button>
        //       </div>
        //     );
        //   }
        // },
        // {
        //   title: '经销商代码',
        //   dataIndex: 'dealerCode',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '经销商名称',
        //   dataIndex: 'dealerName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimColor',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '年款',
          dataIndex: 'yearModel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6048'),
          render: p => {
            return <span>{this.createDictText(p.row.yearModel, '6048')}</span>;
          }
        },
        {
          title: '排量',
          dataIndex: 'disPlacement',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6050'),
          render: p => {
            return <span>{this.createDictText(p.row.disPlacement, '6050')}</span>;
          }
        },
        {
          title: '变速箱',
          dataIndex: 'transMission',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6049'),
          render: p => {
            return <span>{this.createDictText(p.row.transMission, '6049')}</span>;
          }
        },
        {
          title: '排放标准',
          dataIndex: 'dischargeStandard',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1510'),
          render: p => {
            return <span>{this.createDictText(p.row.dischargeStandard, '1510')}</span>;
          }
        },
        {
          title: '续航里程',
          dataIndex: 'enduranceMileage',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6051'),
          render: p => {
            return <span>{this.createDictText(p.row.enduranceMileage, '6051')}</span>;
          }
        },
        {
          title: '是否可订购',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '1004')}</span>;
          }
        },
        {
          title: '是否下发',
          dataIndex: 'isIssue',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isIssue, '1004')}</span>;
          }
        },
        {
          title: '市场指导价',
          dataIndex: 'oemDirectivePrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '共平台车指导价',
          dataIndex: 'energyOemDirectivePrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '国补标准',
          dataIndex: 'subsidyAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'noticeNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '启用标记',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isValid, '1004')}</span>;
          }
        },
        {
          title: '启用日期',
          dataIndex: 'startDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '修改人',
          dataIndex: 'updatedBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '修改日期',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '下发日期',
          dataIndex: 'sendDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    async getcarList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        this.selectList = row;
        this.isOpen = false;
      } else {
        this.selectList = [];
        this.isOpen = true;
      }
    },
    async addHandle(type) {
      // let json = Object.assign({}, { CODE: 'CODE_0000077' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let json = this.fetchParams;
          this.exportLoading = true;

          excelList1(json)
            .then(response => {
              exportExcelFile(response.data, '整车物料价格查询');
              this.exportLoading = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          console.log('456');
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    seacherFun(type) {
      if (type == 1) {
        this.isproduct = true;
      } else {
        this.isSearch = true;
      }
    },
    ownerclose(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.isSearch = false;
    },
    changeHandle(params) {
      this.isOpen = true;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.visible = true;
    },
    addcloseHandler() {
      this.addvisible = false;
      this.isOpen = true;
    },
    emitcloseHandler() {
      this.visible = false;
      this.isOpen = true;
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'updatedBy').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'updatedBy').filterItems = empList;
    }
  }
};
</script>
